home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / archiver / mdcd10.zip / TESTD.PAS < prev    next >
Pascal/Delphi Source File  |  1988-10-26  |  7KB  |  134 lines

  1. {---- 10/23/1988  16:20:35 ----}
  2. {$A-,R-,B-,S-,I-,F-,O-,V-,N-,E-,D-,L-}
  3. {$M 1024,0,26000}                                { stack, min heap, max heap }
  4. {----------------------------------------------------------------------------}
  5. {                                                                            }
  6. {                             'TESTD.PAS'                                    }
  7. {                                                                            }
  8. {  This program tests the LZW DeCompression logic in MDCD1213.OBJ.  It also  }
  9. {  illustrates the calling sequence and interfacing methodology.             }
  10. {                                                                            }
  11. {  To run it, enter:                                                         }
  12. {                                                                            }
  13. {    testd compressedfile  outputfile                                        }
  14. {                                                                            }
  15. {  It will decompress a file that was compressed with TESTC.  If the output  }
  16. {  file currently exists, the program will abort.                            }
  17. {                                                                            }
  18. {                                                                            }
  19. {  No validity checking is done to verify the input file or output file      }
  20. {  names.  This is strictly meant to illustrate a very simple use of the     }
  21. {  MDCD1213.OBJ module.                                                      }
  22. {                                                                            }
  23. {----------------------------------------------------------------------------}
  24.  
  25. Uses Dos;                                      { Borland DOS unit            }
  26.  
  27.   {$L MDCD1213.OBJ}              { assembler module for compress/decompress }
  28.  
  29. {$F+}
  30. Function CompressFile(InFile  : Word;     {handle of input file              }
  31.                       OutFile : Word;     {handle of output file             }
  32.                       AbsOfst : LongInt;  {absolute offset to start write at }
  33.                       ReturnR : Pointer;  {return info from CompressFile     }
  34.                       HashSg  : Word;     {segment:0 of allocated hash table }
  35.                       LZWtype : Word      {12 = 12 bit, 13 = 13 bit lzw      }
  36.                      )        : Word;     {-1 = good return                  }
  37.     external {MDCD1213.OBJ} ;
  38. {$F-}
  39.  
  40. {$F+}
  41. Function DeCompressFile(InFile  : Word;   {handle of input file              }
  42.                         OutFile : Word;   {handle of output file             }
  43.                         AbsOfst : LongInt;{absolute offset to start write at }
  44.                         ReturnR : Pointer;{return info from CompressFile     }
  45.                         HashSg  : Word;   {segment:0 of allocated hash table }
  46.                         LZWtype : Word    {12 = 12 bit, 13 = 13 bit lzw      }
  47.                        )        : Word;   {-1 = good return                  }
  48.     external {MDCD1213.OBJ} ;
  49. {$F-}
  50.  
  51. Type
  52.   ReturnRec = Record                      {return info from mdcd1213.obj     }
  53.     FileCrc  : Word;                      {  calculated file crc decompressed}
  54.     FileSize : LongInt;                   {  compressed/decompressed filesize}
  55.   end;                                    {..                                }
  56.  
  57. Const
  58.   HexXlate : Array[0..$F] of Char =       { table to translate word to hex   }
  59.              '0123456789ABCDEF';
  60.  
  61. Var
  62.   InF     : File;         { Input file                                       }
  63.   OutF    : File;         { Output file                                      }
  64.   IHandle : Word;         { Input file handle                                }
  65.   OHandle : Word;         { Output file handle                               }
  66.   HashPtr : Pointer;      { Pointer to LZW hash table allocated on heap      }
  67.   HashSeg : Word;         { Paragraph aligned (segment) of hash table        }
  68.   RC      : Word;         { Return code from compress/decompress function    }
  69.   ReturnR : ReturnRec;    { storage for return info from mdcd1213.obj        }
  70.  
  71. Begin
  72.  
  73. { compress test }
  74.  
  75.   If ( (Length(ParamStr(1)) = 0) or
  76.        (Length(ParamStr(2)) = 0) ) then begin
  77.     WriteLn;
  78.     WriteLn('Usage: testd inputfile outputfile');
  79.     WriteLn;
  80.     Halt(1);
  81.   end;
  82.  
  83.   Assign(InF, ParamStr(1));       {setup input file variable FileRec         }
  84.   Assign(OutF, ParamStr(2));      {setup output file variable FileRec        }
  85.   Reset(InF,1);                   {open input file to assign handle          }
  86.   If (IOResult <> 0) then begin   {if file didn't open ok..                  }
  87.     WriteLn('Input File not found');{  user error message                    }
  88.     WriteLn('Program Canceled'); {  cancel message                          }
  89.     Halt(1);                      {  end program                             }
  90.   end;                            {..                                        }
  91.   Reset(OutF,1);                  {open output file to check for existance   }
  92.   If (IOResult = 0) then begin    {if file opened ok..                       }
  93.     Close(OutF);                  {close output file                         }
  94.     WriteLn('Output File exists');{  user error message                      }
  95.     WriteLn('Program Canceled'); {  cancel message                          }
  96.     Halt(1);                      {  end program                             }
  97.   end;                            {..                                        }
  98.   ReWrite(OutF,1);                {create/open output file to assign handle  }
  99.   IHandle := FileRec(InF).Handle; {extract input file handle from FileRec    }
  100.   OHandle := FileRec(OutF).Handle;{extract output file handle from FileRec   }
  101.   GetMem(HashPtr, 24576+15);      {get memory for hash table + 15 bytes      }
  102.   if Ofs(HashPtr^) = 0 then       {if offset already -0- then seg is okay    }
  103.     HashSeg := Seg(HashPtr^)      {  store segment                           }
  104.   else                            {else                                      }
  105.     HashSeg := 1+Seg(HashPtr^);   {  add 1 paragraph (16 bytes) to segment   }
  106.  
  107. { DeCompress the file }
  108.  
  109.   RC := DeCompressFile(Ihandle,   { input file handle                        }
  110.                        Ohandle,   { output file handle                       }
  111.                        0,         { file absolute offset in bytes to start   }
  112.                        @ReturnR,  { address of return data from compress     }
  113.                        HashSeg,   { segment of allocated hash table          }
  114.                        13);       { 13 bit decompression                     }
  115.  
  116. { free memory and close files }
  117.  
  118.   FreeMem(HashPtr, 24576+15);     {get rid of heap memory for hash table     }
  119.   Close(InF);                     {close input file                          }
  120.   Close(OutF);                    {close output file                         }
  121.  
  122.   WriteLn;
  123.   Write('DeCompressed file size: ', ReturnR.FileSize);
  124.   Write('  CRC: ');
  125.   Write(HexXlate[Hi(ReturnR.FileCrc) shr 4]);
  126.   Write(HexXlate[Hi(ReturnR.FileCrc) and $F]);
  127.   Write(HexXlate[Lo(ReturnR.FileCrc) shr 4]);
  128.   Write(HexXlate[Lo(ReturnR.FileCrc) and $F]);
  129.   WriteLn;
  130.   WriteLn;
  131.  
  132. End.
  133.  
  134.